﻿package com.views
{
    import com.assist.view.*;
    import com.assist.view.controls.*;
    import com.assist.view.guide.*;
    import com.assist.view.info.*;
    import com.assist.view.interfaces.*;
    import com.controllers.*;
    import com.haloer.display.*;
    import com.lang.client.com.views.*;
    import com.protocols.*;

    public class TrainingView extends Base implements IView
    {
        private var _target:ITraining;
        public var roleInfo:RoleInfo;

        public function TrainingView()
        {
            return;
        }// end function

        public function show() : void
        {
            this.panel_show();
            return;
        }// end function

        public function close() : void
        {
            _popup.closeView(this);
            this.onCancelForGuide();
            return;
        }// end function

        public function clear() : void
        {
            return;
        }// end function

        private function loadComplete() : void
        {
            if (this._target == null)
            {
                this._target = _view.getAssetsObject("Training", "MCTraining") as ITraining;
                this._target.onClose = this.close;
                this._target.onPeiyang = this.onPeiyang;
                this._target.onSave = this.onSave;
                this._target.onCancel = this.onCancel;
                this._target.tfIngot1.htmlText = TrainingViewLang.Training1 + HtmlText.format(TrainingViewLang.Ingot2, 15963904);
                this._target.tfIngot2.htmlText = TrainingViewLang.Training2 + HtmlText.format(TrainingViewLang.Ingot10, 15963904);
                this._target.tfIngot3.htmlText = TrainingViewLang.Training3 + HtmlText.format(TrainingViewLang.Ingot20, 15963904);
                this._target.tfIngot4.htmlText = TrainingViewLang.Training4 + HtmlText.format(TrainingViewLang.Ingot100, 15963904);
                _view.drag.iDrag.addTarget(this._target.dragTitleBar, this._target.content);
            }
            _popup.addView(this, this._target.content);
            var _loc_1:* = new Image(this.roleInfo.headUrl);
            _loc_1.onComplete = this.laodHeadIconComplete;
            this._target.headIcon = _loc_1;
            this._target.tfName.text = this.roleInfo.name;
            this._target.tfLv.text = this.roleInfo.lv + TrainingViewLang.Lv;
            this._target.tfCoin.htmlText = TrainingViewLang.Training0 + this.rolemsg.coin + TrainingViewLang.Coin;
            this._target.radio = this.rolemsg.radio;
            this.updateOldProperty(this.rolemsg.trainingBase, this.rolemsg.trainingOld, this.rolemsg.trainingNew);
            this._view.guide.trackTraining(3, this._target.content);
            return;
        }// end function

        private function laodHeadIconComplete(param1:Image) : void
        {
            var _loc_2:int = 50;
            param1.height = 50;
            param1.width = _loc_2;
            return;
        }// end function

        private function updateOldProperty(param1:Array, param2:Array, param3:Array) : void
        {
            this.roleInfo.pWuLi = param1[0] + param2[0];
            this.roleInfo.pJueJi = param1[1] + param2[1];
            this.roleInfo.pFaShu = param1[2] + param2[2];
            var _loc_4:* = this.roleInfo.lv * 2 + 20;
            var _loc_5:Boolean = true;
            var _loc_6:Array = ["", "", ""];
            var _loc_7:Array = ["", "", ""];
            var _loc_8:Array = [3, 3, 3];
            var _loc_9:int = 0;
            while (_loc_9 < 3)
            {
                
                if (param2[_loc_9] == _loc_4)
                {
                    _loc_6[_loc_9] = param1[_loc_9] + HtmlText.green("+" + param2[_loc_9]) + HtmlText.yellow2(TrainingViewLang.Full);
                }
                else
                {
                    _loc_6[_loc_9] = param1[_loc_9] + HtmlText.green("+" + param2[_loc_9]);
                    _loc_5 = false;
                }
                _loc_9++;
            }
            _loc_9 = param3.length;
            while (_loc_9 > 0)
            {
                
                _loc_9 = _loc_9 - 1;
                if (param3[_loc_9] > param2[_loc_9])
                {
                    _loc_8[_loc_9] = 2;
                    _loc_7[_loc_9] = param1[_loc_9] + HtmlText.green("+" + param3[_loc_9]);
                }
                else if (param3[_loc_9] < param2[_loc_9])
                {
                    _loc_8[_loc_9] = 1;
                    _loc_7[_loc_9] = param1[_loc_9] + HtmlText.red("+" + param3[_loc_9]);
                }
                else
                {
                    _loc_7[_loc_9] = param1[_loc_9] + HtmlText.green("+" + param3[_loc_9]);
                }
                if (param3[_loc_9] == _loc_4)
                {
                    _loc_7[_loc_9] = param1[_loc_9] + HtmlText.green("+" + param3[_loc_9]) + HtmlText.yellow2(TrainingViewLang.Full);
                }
            }
            this._target.setProperty(_loc_6[0], _loc_6[1], _loc_6[2], _loc_7[0], _loc_7[1], _loc_7[2], _loc_8[0], _loc_8[1], _loc_8[2], _loc_5);
            return;
        }// end function

        private function onPeiyang() : void
        {
            this.training(this._target.radioIdx);
            this.onTrainingForGuide();
            return;
        }// end function

        private function onSave() : void
        {
            var cookie:String;
            var checked:Boolean;
            if (_ctrl.rolemsg.hasLower == true)
            {
                cookie = this + "Save";
                checked = _view.getCookie(cookie) == 1;
                if (checked == true)
                {
                    this.modify_role_data();
                }
                else
                {
                    _view.alert.iAlert.hasCheckbox = true;
                    _view.alert.showYesNoMsg(TrainingViewLang.SaveShuXing, TrainingViewLang.Yes, TrainingViewLang.No, function (param1:int) : void
            {
                if (param1 == AlertButtonType.Yes)
                {
                    modify_role_data();
                    if (_view.alert.iAlert.checked == true)
                    {
                        _view.setCookie(cookie, 1);
                    }
                }
                else
                {
                    updateOldProperty(rolemsg.trainingBase, rolemsg.trainingOld, rolemsg.trainingNew);
                }
                return;
            }// end function
            );
                }
            }
            else
            {
                this.modify_role_data();
            }
            return;
        }// end function

        private function onCancel() : void
        {
            this.onCancelForGuide();
            this.updateOldProperty(this.rolemsg.trainingBase, this.rolemsg.trainingOld, []);
            return;
        }// end function

        private function get rolemsg() : RoleMsgController
        {
            return _ctrl.rolemsg;
        }// end function

        private function panel_show() : void
        {
            _data.call(Mod_Training_Base.panel_show, this.panel_show_back, [this.roleInfo.playerRoleId]);
            return;
        }// end function

        private function panel_show_back() : void
        {
            loadAssets("Training", null, TrainingViewLang.LoadTrainingTip, false, false, this.loadComplete);
            return;
        }// end function

        private function training(param1:int) : void
        {
            _data.call(Mod_Training_Base.training, this.training_back, [this.roleInfo.playerRoleId, param1]);
            return;
        }// end function

        private function training_back() : void
        {
            this.updateOldProperty(this.rolemsg.trainingBase, this.rolemsg.trainingOld, this.rolemsg.trainingNew);
            if (this.rolemsg.result == Mod_Training_Base.SUCCEED)
            {
            }
            else if (this.rolemsg.result == Mod_Training_Base.NOENOUGHCOINS)
            {
                _view.showTip(TrainingViewLang.CoinLack);
            }
            else if (this.rolemsg.result == Mod_Training_Base.NOENOUGHINGOT)
            {
                _view.showTip(TrainingViewLang.IngotLack);
            }
            else
            {
                _view.showTip(TrainingViewLang.TrainingFails);
            }
            return;
        }// end function

        private function modify_role_data() : void
        {
            _data.call(Mod_Training_Base.modify_role_data, this.modify_role_data_back, [this.roleInfo.playerRoleId]);
            return;
        }// end function

        private function modify_role_data_back() : void
        {
            if (this.rolemsg.result == Mod_Training_Base.SUCCEED)
            {
                _view.showTip(TrainingViewLang.SaveSucess, "", TipType.Success);
                this.updateOldProperty(this.rolemsg.trainingBase, this.rolemsg.trainingOld, this.rolemsg.trainingNew);
                this._target.playSaveOK();
                this._view.roleMsg.updateSimpe();
                this.onCancelForGuide();
            }
            else
            {
                _view.showTip(TrainingViewLang.TrainingFails);
            }
            return;
        }// end function

        private function onTrainingForGuide() : void
        {
            this._view.guide.trackTraining(4, this._target.content);
            return;
        }// end function

        private function onCancelForGuide() : void
        {
            _view.guide.setCookie(GuideType.Training);
            _view.guide.stopTrainingGuides();
            return;
        }// end function

    }
}
